//
//  WHWalletController.m
//  SquirrelSeller
//
//  Created by 邓伟浩 on 2018/12/29.
//  Copyright © 2018 邓伟浩. All rights reserved.
//

#import "WHWalletController.h"
#import "WHMoneyTopView.h"
#import "WHWalletMenuCell.h"
#import "WHMoneyDetailController.h"
#import "WHPackageBillController.h"
#import "WHGetMoneyController.h"
#import "WHPayPasswordController.h"
#import "WHBankInfoController.h"

@interface WHWalletController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UIImageView *topBackImageView;
@property (nonatomic, strong) WHMoneyTopView *topView;
@property (nonatomic, strong) UICollectionView *menuSelectView;

@property (nonatomic, strong) NSArray *tipsImageArray;
//@property (nonatomic, strong) NSArray *tipsNameArray;

@end

@implementation WHWalletController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self selectBalance];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"余额";
    self.naviType = NaviTypeClear;
    [self initParams];
    [self setUpUI];
}

- (void)initParams {
    _tipsImageArray = @[@"mywallet_bill_icon", @"mywallet_bank_icon", @"mywallet_transDetail_icon", @"mywallet_payPassword_icon"];
//    _tipsNameArray = @[@"包裹寄存账单", @"银行卡", @"支付密码"];
}

- (void)setUpUI {
    [self setUpBackView];
    [self setUpBottomView];
    [self setUpTopView];
}

- (void)setUpBackView {
    [self.view addSubview:({
        _topBackImageView = [[UIImageView alloc] init];
        _topBackImageView.contentMode = UIViewContentModeScaleAspectFill;
        _topBackImageView.userInteractionEnabled = YES;
        _topBackImageView.image = IMAGE_NAMED(@"mywallet_back_img");
        
        _topBackImageView;
    })];
    [_topBackImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(self.view);
        if (@available(iOS 11.0, *)) {
            make.bottom.mas_equalTo(self.view.mas_safeAreaLayoutGuideBottom);
        } else {
            make.bottom.mas_equalTo(self.view.mas_bottom).offset(-kTabBarHeight);
        }
    }];
    _topBackImageView.clipsToBounds = YES;
}

- (void)setUpTopView {
    
    WHMoneyTopView *topView = [[WHMoneyTopView alloc] init];
    [self.view addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_top).offset(kTopHeight);
        make.left.right.mas_equalTo(self.view);
        make.bottom.mas_equalTo(_menuSelectView.mas_top);
    }];
    _topView = topView;
    kWeakSelf(self);
    topView.balanceTap = ^{
        [weakself toGetMoney];
    };
    
}

- (void)setUpBottomView {
    UIView *bottomView = [[UIView alloc] init];
    [self.view addSubview:bottomView];
    bottomView.backgroundColor = KWhiteColor;
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.height.mas_equalTo(kRealwidth(200));

        if (@available(iOS 11.0, *)) {
            make.bottom.mas_equalTo(self.view.mas_safeAreaLayoutGuideBottom);
        } else {
            make.bottom.mas_equalTo(self.view.mas_bottom).offset(-kTabBarHeight);
        }
    }];
    [self.view layoutIfNeeded];
    
    UIImageView *roundImageView = [[UIImageView alloc] init];
    [self.view addSubview:roundImageView];
    roundImageView.image = IMAGE_NAMED(@"mywallet_roundBack");
    [roundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.height.mas_equalTo(kRealwidth(30));
        make.bottom.mas_equalTo(bottomView.mas_top);
    }];
    
//    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:bottomView.bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(12 ,12)];
//    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
//    maskLayer.frame = bottomView.bounds;
//    maskLayer.path = maskPath.CGPath;
//    bottomView.layer.mask = maskLayer;
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(KScreenWidth/2-0.5, kRealwidth(90));
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 1;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    UICollectionView *menuSelectView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    menuSelectView.backgroundColor = CLineColor;
    menuSelectView.dataSource = self;
    menuSelectView.delegate = self;
    menuSelectView.bounces = NO;
    
    [menuSelectView registerClass:[WHWalletMenuCell class] forCellWithReuseIdentifier:NSStringFromClass([WHWalletMenuCell class])];
    
    [bottomView addSubview:menuSelectView];
    [menuSelectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(bottomView);
        make.centerY.mas_equalTo(bottomView.mas_centerY);
        make.height.mas_equalTo(kRealwidth(180)+1);
    }];
    
    _menuSelectView = menuSelectView;
}


#pragma mark —————— UICollectionViewDataSource & UICollectionViewDelegate ——————

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _tipsImageArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    WHWalletMenuCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([WHWalletMenuCell class]) forIndexPath:indexPath];
    cell.tipsImageView.image = IMAGE_NAMED(_tipsImageArray[indexPath.row]);
//    cell.tipsLabel.text = _tipsNameArray[indexPath.row];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        WHPackageBillController *packageBill = [[WHPackageBillController alloc] init];
        [self.navigationController pushViewController:packageBill animated:YES];
    } else if (indexPath.row == 1) {
        WHBankInfoController *bankInfo = [[WHBankInfoController alloc] init];
        [self.navigationController pushViewController:bankInfo animated:YES];
    } else if (indexPath.row == 2) {
        WHMoneyDetailController *moneyDetail = [[WHMoneyDetailController alloc] init];
        [self.navigationController pushViewController:moneyDetail animated:YES];
    } else if (indexPath.row == 3) {
        WHPayPasswordController *payPassword = [[WHPayPasswordController alloc] init];
        [self.navigationController pushViewController:payPassword animated:YES];
    }
}


#pragma mark - —————— 事件 ——————
- (void)transDetail:(UIButton *)button {
    WHMoneyDetailController *moneyDetail = [[WHMoneyDetailController alloc] init];
    [self.navigationController pushViewController:moneyDetail animated:YES];
}

- (void)toGetMoney {
    if (userManager.detailUserInfo.isPayPassWord) {
        WHGetMoneyController *getMoney = [[WHGetMoneyController alloc] init];
        [self.navigationController pushViewController:getMoney animated:YES];
    } else {
        [WHView showAlertwithTitle:nil message:@"请先设置支付密码" okAction:^(UIAlertAction *action) {
            WHPayPasswordController *payPassword = [[WHPayPasswordController alloc] init];
            [self.navigationController pushViewController:payPassword animated:YES];
        } cancelAction:nil];
    }
}

#pragma mark - —————— 接口 ——————
- (void)selectBalance {
    NSString *postURL = NSStringFormat(@"%@%@", URL_main, URL_selectBanlance);
    
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                          @(userManager.detailUserInfo.userId), @"userId",
                          nil];
    
    [WHNetworkManager requestWithURL:postURL parameters:dict Success:^(id responseObject) {
        DLog(@"%@", responseObject);
        if (ValidDict(responseObject)) {
            NSNumber *code = responseObject[@"errcode"];
            NSString *errorMsg = responseObject[@"errmsg"];
            if (code.intValue == 0) {
                NSDictionary *retDict = responseObject[@"retobj"];
                NSString *balance = retDict[@"balance"];
                _topView.balanceMoney = balance;
                userManager.balance = balance;
                
            } else {
                [WHView showHudMsg:errorMsg];
            }
        }
    } failure:^(NSError *error) {
        
    }];
}

@end
